<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Menu;
use app\common\model\UserLevel;
use app\common\validate\Admin;
use think\facade\Request;
use think\facade\View;
use think\exception\ValidateException;

class AdminRbac extends AdminInit
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        View::assign("title","菜单列表");
        return  view();
    }

    public function getmenus()
    {
        $id = input("param.id");
        if($id){
            $power = UserLevel::find($id);
            if($power["power"]){
                $power = json_decode($power["power"]);
            }else{
                $power = [];
            }
        }else{
            $power = [];
        }



        $menu  = Menu::select()->toArray();
        foreach ($menu as $k => $v){
            if($v["module_name"] && $v["action_name"]){
                $menu[$k]["url"] = url($v["module_name"]."/".$v["action_name"]);
            }else{
                $menu[$k]["url"] = "javascript:;";
            }
            $menu[$k]["open"] = false;
            foreach ($power as $m => $n){
                if($v["id"] == $n){
                    $menu[$k]["checked"] = true;
                    break;
                }
            }
        }
        $menu = unlimitforlayer($menu,"children");
        $parent = [
            "name"=>"全选/取消",
            "open"=>true,
            "children"=>$menu
        ];
        return json($parent);
    }

    //添加一个菜单节点
    public function addnode()
    {

        $pid = input("get.pid");

        $type = input("get.type");
        if($type == 'edit'){
            $res = Menu::find($pid)->toArray();

        }else{
            $res = [];
        }

        $menu  = Menu::select()->toArray();
        foreach ($menu as $k => $v){
            if($v["module_name"] && $v["action_name"]){
                $menu[$k]["url"] = url($v["module_name"]."/".$v["action_name"]);
            }else{
                $menu[$k]["url"] = "javascript:;";
            }
            $menu[$k]["open"] = true;
            if($pid == $v["id"]){
                $menu[$k]["checked"] = 1;
            }else{
                $menu[$k]["checked"] = 0;
            }
        }

        $menu = unlimitforlayer($menu,"children");


        View::assign("tmenu",$menu);
        View::assign("title","菜单列表");
        View::assign("res",$res);
        return  view();
    }

    //
    public function add()
    {
          $data = input("post.");
          $mod = new  Menu();
          if(isset($data["id"])){
              $res = $mod->update($data);
          }else{
              $res = $mod->save($data);
              //仅仅在添加 方法为index的时候  同时添加 create  save edit update delete
              $all = [];


              //公共数据
              $data["display"] = 0;
              $data["often"] = 0;
              $data["pid"] = $mod->id;

              $data["name"] = "保存修改";
              $data["action_name"] = "create";
              array_push($all,$data);

              $data["name"] = "修改页面";
              $data["action_name"] = "edit";
              array_push($all,$data);

              $data["name"] = "删除";
              $data["action_name"] = "delete";
              array_push($all,$data);

              //$data["name"] = "保存修改";
              //$data["action_name"] = "save";
              //array_push($all,$data);
              //
              //$data["name"] = "修改";
              //$data["action_name"] = "update";
              //array_push($all,$data);
              //以上2方法暂时不需要 需要时进行添加
              //p($all);die;
              $menu = new  Menu();
              $menu->saveAll($all);
          }
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function del($id)
    {
        $data = input("post.");
        $id = $data["id"];
        $res = Menu::where("id",$id)->delete();
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }

    }

    public function group()
    {

        $where["admin"] = 1;

        $list  = UserLevel::where($where)->select();
        //        父级分类
        View::assign("list",$list);
        View::assign("title","后台管理员分组");

        return  view();
    }

    //添加分组
    public function addgroup()
    {
        if(Request::isAjax()){
            $data = input("post.");
            if(!$data["name"]){
                return  json(["code"=>4000,"msg"=>"分组名必填"]);
            }
            $res = UserLevel::create($data);
            if($res){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }
        }
        $type = input("get.type","add");
        $id = input("get.id");
        if($type == "edit"){
            $res = UserLevel::where("id",$id)->find();
        }else{
            $res = [];
        }
        View::assign("res",$res);
        return  view();
    }

    public function delgroup()
    {
        $id = input("post.id");
        $res = UserLevel::where("id",$id)->delete();
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

    //授权
    public function grouppower()
    {
        if(Request::isAjax()){
            $d = input("post.ids");
            $d = explode(",",$d);
            $user_level_id = input("post.user_level_id");
            foreach ($d as $k => $v){
                if($v == "undefined" || !$v){
                    unset($d[$k]);
                }
            }
            $res = UserLevel::where("id",$user_level_id)
                ->save(["power"=>json_encode($d)]);
            if($res){
                return  json(["code"=>200,"msg"=>"成功"]);
            }else{
                return  json(["code"=>4000,"msg"=>"请稍后再试"]);
            }
        }
        $menu  = Menu::select()->toArray();

        //查询用户已有权限
        $id = input("get.id");
        $power = UserLevel::where("id",$id)->field("power")->find()->toArray();

        if($power["power"]){
            $power = json_decode($power["power"],true);
        }

        foreach ($menu as $k => $v){
            if($v["module_name"] && $v["action_name"]){
                $menu[$k]["url"] = url($v["module_name"]."/".$v["action_name"]);
            }else{
                $menu[$k]["url"] = "javascript:;";
            }
            $menu[$k]["open"] = true;

            //判断用户权限
            foreach ($power as $m => $n){
                if($v["id"] == $n){
                    $menu[$k]["checked"] = true;
                    break;
                }
            }
        }
        //
        $menu = unlimitforlayer($menu,"children");
        $parent = [
            "name"=>"全选/取消",
            "open"=>true,
            "children"=>$menu,
            "checked" => true
        ];
        //p($parent);die;

        View::assign("menu",$parent);
        View::assign("title","菜单列表");
        return  view();
    }

    public function addadmin()
    {
        if(\request()->isAjax()){
            $data = \request()->post();
            if($data["level_id"]==0){
                return json(["code"=>203,"msg"=>"用户组不能为空"]);
            }
            try {
                validate(Admin::class)->check($data);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return json(["code"=>203,"msg"=>$e->getError()]);
            }
            $data["is_admin"] = 1;
            $data["password"] = md5($data["password"]);
            $res = \app\common\model\User::create($data);
            if($res){
                return json(["code"=>200,"msg"=>"成功"]);
            }else{
                return json(["code"=>201,"msg"=>"失败"]);
            }
        }

        $list = UserLevel::where("admin",1)
            ->field("id,name,status")
            ->select();

        View::assign("list",$list);
        View::assign("res",[]);
        return  view();
    }

}
